package org.front.client.widget.mainwidget;

import java.util.ArrayList;
import java.util.List;

import org.front.client.widget.beneficewidget.BeneficeWidget;
import org.front.client.widget.distributeurwidget.DistributeurWidget;
import org.front.client.widget.listeproduitwidget.ListeProduitWidget;
import org.front.client.widget.listetransactionwidget.ListeTransactionWidget;
import org.front.client.widget.mainwidget.event.MainWidgetEvent;
import org.front.client.widget.mainwidget.event.MainWidgetHandler;
import org.front.client.widget.mainwidget.i18n.images.MainWidgetImages;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

public class MainWidget extends Composite implements MainWidgetHandler {

	@UiField
	FlowPanel rootPanel;

	@UiField
	FlowPanel loading;
	
	@UiField
	FlowPanel contenu;

	@UiField (provided = true)
	DistributeurWidget distributeurWidget;
	
	@UiField (provided = true)
	ListeTransactionWidget listeTransactionWidget;
	
	@UiField (provided = true)
	ListeProduitWidget listeProduitWidget;
	
	@UiField (provided = true)
	BeneficeWidget beneficeWidget;
	

	/** event bus on which to commit */
	private final EventBus eventBus;

	/** Handlers the widget registers */
	private final List<HandlerRegistration> handlersRegistration;

	/**
	 * Client Bundle resources for this widget
	 */
	public interface Resources extends ClientBundle {

		/** Instance */
		public Resources INSTANCE = GWT.create(Resources.class);

		/**
		 * @return Css
		 */
		@Source("MainWidget.css")
		MainWidgetCssResources css();

		/**
		 * @return Images du widget
		 */
		MainWidgetImages images();
	}

	/**
	 * Declare UIBinder for this widget
	 */
	@UiTemplate("MainWidget.ui.xml")
	interface MainWidgetUiBinder extends UiBinder<Widget, MainWidget> {
	}

	/** UIBinder for building interface */
	private final MainWidgetUiBinder uiBinder;

	public MainWidget(EventBus eventBus, DistributeurWidget distributeurWidget, ListeTransactionWidget listeTransactionWidget, ListeProduitWidget listeProduitWidget, BeneficeWidget beneficeWidget) {

		this.beneficeWidget = beneficeWidget;
		this.distributeurWidget = distributeurWidget;
		this.listeProduitWidget = listeProduitWidget;
		this.listeTransactionWidget = listeTransactionWidget;
		
		this.eventBus = eventBus;

		// registration of handlers
		this.handlersRegistration = new ArrayList<HandlerRegistration>();

		// create UI
		this.uiBinder = GWT.create(MainWidgetUiBinder.class);
		initWidget(this.uiBinder.createAndBindUi(this));
		Resources.INSTANCE.css().ensureInjected();
		rootPanel.setStyleName("main");
	}

	@Override
	public void onMainWidget(MainWidgetEvent event) {

		this.contenu.clear();

		GWT.log("onMainWidget : " + event.getContext().getAction());

		if (event.getContext().getAction() == MainWidgetEvent.Context.Action.AFFICHER_DISTRIBUTEUR) {
			distributeurWidget.initialiserData();
			this.contenu.add(distributeurWidget);

		} else if (event.getContext().getAction() == MainWidgetEvent.Context.Action.AFFICHER_TRANSACTION) {
			listeTransactionWidget.initialiserData();
			this.contenu.add(listeTransactionWidget);			

		} else if (event.getContext().getAction() == MainWidgetEvent.Context.Action.AFFICHER_MEILLEUR_PRODUIT) {
			listeProduitWidget.initialiserData();
			this.contenu.add(listeProduitWidget);
		}

		else if (event.getContext().getAction() == MainWidgetEvent.Context.Action.AFFICHER_BENEFICE_MOIS) {
			beneficeWidget.initialiserData();
			this.contenu.add(beneficeWidget);
		}

		this.contenu.setVisible(true);
	}

	@Override
	protected void onLoad() {

		super.onLoad();
		bind();
	}

	@Override
	protected void onUnload() {
		super.onUnload();
		unbind();
	}

	/**
	 * Bind handler
	 */
	protected void bind() {
		this.handlersRegistration.add(this.eventBus.addHandler(MainWidgetEvent.TYPE, this));
	}

	/**
	 * Unbing handler
	 */
	protected void unbind() {
		// remove handlers registration
		for (final HandlerRegistration hr : this.handlersRegistration) {
			hr.removeHandler();
		}
		this.handlersRegistration.clear();
	}
}
